//
// Created by 成飞 on 2024/1/8.
//

#ifndef ORDERKV_META_DATA_H
#define ORDERKV_META_DATA_H

#include <vector>
#include <map>
#include <set>
#include <atomic>
#include <mutex>
#include "util/lock.hpp"
#include <fstream>

#define FILE_NO_MAX 4096

namespace meta{
    typedef uint32_t fid;
    typedef uint32_t f_off; //数据偏移量，如果是int则一个文件不超过4GB

    extern std::atomic<fid> readyFileNo;
    extern std::atomic_bool closeDB;
    extern std::mutex mtx;
    extern std::mutex mtx4range;

    extern std::string obsoleteFidLoggerPathStr;
    extern std::ofstream obsoleteFidLogger;
    extern std::string fidAddrLoggerPathStr;
    extern std::ofstream fidAddrLogger;

    struct ControlInfo{
        std::string addr;
        std::atomic<uint32_t> readers;
        size_t garbageSize;
        std::vector<f_off> invalidValOffsets;
        FILE* fd; //vFile的描述符全部常驻内存，优化读！
    };

    extern std::map<fid, ControlInfo> fileControlInfo;

    extern std::array<std::string_view, FILE_NO_MAX> buffers;

    void init();

    void load();

    void storeInfo();

    void loadInfo();

    void initLog();

    void closeLog();

    void clearLog();

    void repairMeta();
}

//void meta_data_persistence();
//gc

/**
 *  |readyFileNo|
 *
 */

#endif //ORDERKV_META_DATA_H
